using System.Windows.Media;
using Crusades.MapView;

namespace Crusades.Map {
  public class MapController {
    private readonly IModernMap view;
    private ScaleTransform scaleTransform;
    private const double applicationWidth = 1024;
    private const double applicationHeight = 768;
    private TranslateTransform translateTransform;
    private readonly CountryList countryList;

    public MapController(IModernMap view) {
      this.view = view;
      setupTransform();
      countryList = new CountryList();
      countryList.hookupCountries(view);
    }

    public void Zoom(double zoomLevel) {
      scaleTransform.ScaleX = zoomLevel;
      scaleTransform.ScaleY = zoomLevel;
      scaleTransform.CenterX = applicationWidth / 2;
      scaleTransform.CenterY = applicationHeight / 2;
      countryList.SetLineWeight(1/zoomLevel);
    }

    public void Pan(double x, double y) {
      translateTransform.X = x;
      translateTransform.Y = y;
    }

    private void setupTransform() {
      scaleTransform = new ScaleTransform
                       {
                         CenterX = applicationWidth / 2,
                         CenterY = applicationHeight / 2,
                         ScaleX = 1,
                         ScaleY = 1
                       };
      translateTransform = new TranslateTransform { X = 0, Y = 0 };
      var group = new TransformGroup();
      group.Children.Add(scaleTransform);
      group.Children.Add(translateTransform);
      view.RenderTransform = group;
    }
  }
}